home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news.new-york.net!actcom!news
- From: goren@shani.net (Redrum)
- Subject: Re: random numbers
- X-Nntp-Posting-Host: teleport39.shani.co.il
- Message-ID: <DoDG4G.2zz@actcom.co.il>
- Sender: news@actcom.co.il (News)
- Organization: ACTCOM - Internet Services in Israel
- X-Newsreader: Forte Free Agent 1.0.82
- References: <4ickm0$m8l@nova.umuc.edu>
- Date: Sat, 16 Mar 1996 17:46:52 GMT
-
- srussell@nova.umuc.edu (Steve Russell) wrote:
-
- >I just got a project from an instructor for what should be a
- >simple program --- simulating the roll of the dice. The whole
- >thing is executing the stdlib.h function rand() ( rand()%6+1)
- >to get a random number between 1 & 6.
-
- >The problem is I keep getting the *same* number. I didn't
- >read the library description, I don't have my book with me
- >and it is a 45min walk home. I assumed it was of type int
- >and needed no inputs.
-
- >Does anybody know anything about this function?
-
- >Thanks
- >Steve
-
- Steve,
- This happens because of a pecularity of the rand() fn. The computer
- doesn't really generate a random number - it uses a fixed list of
- random numbers!
-
- The easiest way to make it "random" is to use the randomize() macro.
-
- This is a macro which initializes the random number generator with a
- random value. Since randomize() does it by calling the time() fn,
- you'll need to #include <time.h> in your program, then place the line:
- randomize();
- before you start the loop.
-
- HTH,
-
-
- Nave Goren <goren@shani.net>
- -"Of course my Cabbage needs no introduction"...
-
-